package ex3;

import ex2.grammars.GrammarType;
import ex3.visitors.REPL;
import ic.ast.decl.Program;

/**
 * Created with IntelliJ IDEA.
 * User: rokli_000
 * Date: 12/1/13
 */
public class Interpreter extends Ex3Runnable {

    private String programFilename;
    private String libraryFilename;
    private String className;
    private String methodName;
    private String[] methodParameters;

    private Interpreter() {}

    @Override
    public void run() throws Exception {
        Program ast = (Program) getAst(programFilename, GrammarType.GRAMMAR_TYPE_PROGRAM);

        if(libraryFilename != null) {
            Program libAst = (Program) getAst(libraryFilename, GrammarType.GRAMMAR_TYPE_LIBRARY);
            ast.getClasses().add(0, libAst.getClasses().get(0));
        }

        ast.accept(new REPL(className, methodName, methodParameters));
    }

    /**
     * Builder inner class
     */
    public static class Builder {
        private Interpreter interpreter = new Interpreter();

        public Builder withProgramFilename(String programFilename) {
            interpreter.programFilename = programFilename;
            return this;
        }

        public Builder withLibraryFilename(String libraryFilename) {
            interpreter.libraryFilename = libraryFilename;
            return this;
        }

        public Builder withClass(String className) {
            interpreter.className = className;
            return this;
        }

        public Builder withMethod(String methodName, String... methodParameters) {
            interpreter.methodName = methodName;
            interpreter.methodParameters = methodParameters;
            return this;
        }

        public Interpreter build() {
            return interpreter;
        }
    }
}
